from flask import Blueprint, jsonify, request

from service.ArticleService import assemble_article_by_request, save_article, assemble_edit_article_by_request, \
    update_article, assemble_about_me, update_about_me, get_service_article_by_uuid, update_article_comment_count
from service.CommentService import get_article_comment, assemble_article_comment, save_comment

api_article = Blueprint('api_article', __name__)
api_article.url_prefix = '/api/article'


@api_article.route('', methods=['POST'])
def upload_blog():
    article = assemble_article_by_request()
    save_article(article)
    return jsonify({'data': "success"})


@api_article.route('/edit_article', methods=['POST'])
def save_edit_blog():
    article = assemble_edit_article_by_request()
    update_article(article)
    return jsonify({'data': "success"})


@api_article.route('/edit_about_me', methods=['POST'])
def edit_about_me():
    article = assemble_about_me()
    update_about_me(article)
    return jsonify({'data': "success"})


@api_article.route('/comment/<uuid>', methods=['GET'])
def get_article_comment_by_uuid(uuid):
    comments = get_article_comment(uuid)
    return {"code": 200, "msg": "success", "data": comments}


@api_article.route('/comment', methods=['POST'])
def save_article_comment_by_uuid():
    comment = assemble_article_comment()
    save_comment(comment)
    article = get_service_article_by_uuid(request.json['uuid'])
    update_article_comment_count(article)
    data = get_article_comment(request.json['uuid'])
    return {"code": 200, "msg": "success", "data": data}
